﻿Imports System.Configuration
Namespace helper
    ''' <summary>
    ''' Provides methods for retrieve value from config file AppSettingsSection
    ''' </summary>
    ''' <remarks></remarks>
    Public Class AppSettingHelper
        'created by : Sum
        'created date: 9/10/2011
        'description: this class is use to read appSetting in web.config

        ''' <summary>
        ''' Gets the AppSettingsSection data for the current application's default configuration.
        ''' </summary>
        ''' <param name="keyName">key of the entry to locate.the key cannot be null.</param>
        ''' <returns>value of specified key of the entry</returns>
        ''' <remarks></remarks>
        Public Shared Function GetString(ByVal keyName As String) As String
            Return AppSettingHelper.GetString(keyName, False, String.Empty)
        End Function

        ''' <summary>
        ''' Gets the AppSettingsSection data for the current application's default configuration.
        ''' </summary>
        ''' <param name="keyName">key of the entry to locate.the key cannot be null.</param>
        ''' <param name="mustExist">whether key of the entry must exist</param>
        ''' <param name="defaultVal"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetString(ByVal keyName As String, ByVal mustExist As Boolean, ByVal defaultVal As String) As String
            If String.IsNullOrEmpty(keyName) Then
                Throw New ArgumentNullException("keyName")
            End If
            Dim str As String = ConfigurationManager.AppSettings.Item(keyName)
            If (Not str Is Nothing) Then
                Return str
            End If
            If mustExist Then
                Throw New Exception(keyName & "does not exist in app setting.")
            End If
            Return defaultVal
        End Function
    End Class
End Namespace

